﻿using UnityEngine;
using System.Collections;

namespace EF4
{
	public static class MathUtil 
	{
		/// <summary>
		/// 获取一个二维向量的垂直向量, clockwise = True 顺时针,否则,逆时针方向
		/// </summary>
		public static Vector2 GetVerticalVec(Vector2 sourceVec,bool clockwise){
			// 向量垂直, x1 * x2 + y1 * y2 = 0; 
			// 1.置换x,y.  2.决定正负
			Vector2 res = new Vector2(sourceVec.y,sourceVec.x);//
			if (clockwise) {
				//顺时针
				res.y = -res.y;
			} else {
				//逆时针
				res.x = -res.x;
			}
			return res;
		}

		/// <summary>
		/// 求平面内向量 sourceVec ,逆时针旋转 angle 后的向量
		/// </summary>
		public static Vector3 RotateVector2(Vector2 sourceVec,float angle){
			Vector2 res = Vector2.zero;
			float sinV = Mathf.Sin (angle * Mathf.Deg2Rad);
			float cosV = Mathf.Cos (angle * Mathf.Deg2Rad);
			res.x = sourceVec.x * cosV - sourceVec.y * sinV;
			res.y = sourceVec.x * sinV + sourceVec.y * cosV;
			return res;
		}

		/// <summary>
		/// 求平面内向量 sourceVec ,逆时针旋转 angle 后的向量
		/// </summary>
		public static Vector2 RotateVector2(float x,float y,float angle){
			return RotateVector2 (new Vector2(x,y),angle);
		}

		/// <summary>
		/// p : 点/方向/向量， n ：旋转轴， angle ： 旋转角度
		/// </summary>
		public static Vector3 RotateVector3(Vector3 p,Vector3 n,float angle){
			
			float sinV = Mathf.Sin (angle * Mathf.Deg2Rad);
			float cosV = Mathf.Cos (angle * Mathf.Deg2Rad);
			Vector3 res = Vector2.zero;
			float x = p.x;
			float y = p.y;
			float z = p.z;

			float nx = n.x;
			float ny = n.y;
			float nz = n.z;
			res.x = x * ( nx * nx * ( 1 - cosV) + cosV )
					+ y * ( nx * ny * ( 1 - cosV) - nz * sinV )
					+ z * ( nx * nz * ( 1 - cosV) + ny * sinV );

			res.y = x * ( nx * ny * ( 1 - cosV ) + nz * sinV )
					+ y * ( ny * ny * ( 1 - cosV ) + cosV )
					+ z * ( ny * nz * ( 1 - cosV ) - nx * sinV );

			res.z = x * ( nx * nz * ( 1 - cosV ) - ny * sinV )
					+ y * ( ny * nz * ( 1 - cosV ) + nx * sinV )
					+ z * ( nz * nz * ( 1 - cosV ) + cosV );				

			return res;
		}


	}
}
